package com.gly091020.GLYCan.item;

import com.gly091020.GLYCan.GLYCanMod;
import com.gly091020.GLYCan.block.CanBlock;
import com.gly091020.GLYCan.block.entity.CanBlockEntity;
import com.gly091020.GLYCan.util.CanComponent;
import com.gly091020.GLYCan.util.CanItemContainer;
import net.minecraft.core.component.DataComponents;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.ItemTags;
import net.minecraft.world.ContainerHelper;
import net.minecraft.world.ItemInteractionResult;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.SlotAccess;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.food.FoodProperties;
import net.minecraft.world.inventory.ClickAction;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.PotionItem;
import net.minecraft.world.item.alchemy.PotionContents;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.Optional;

public class CanItem extends BlockItem {
    public CanItem(Block block, Properties properties) {
        super(block, properties);
    }

    @Override
    protected boolean placeBlock(BlockPlaceContext context, BlockState state) {
        if(context.getPlayer() != null && context.getPlayer().isShiftKeyDown()){
            var d = context.getItemInHand().getOrDefault(GLYCanMod.CAN_COMPONENTS,
                    new CanComponent(false, CanBlock.NORMAL_COLOR));
            var r = super.placeBlock(context, state.setValue(CanBlock.IS_TETO, d.isTeto()));
            if(context.getLevel().getBlockEntity(context.getClickedPos()) instanceof CanBlockEntity entity){
                entity.color = d.color();
            }
            return r;
        }
        return false;
    }

    @Override
    public @Nullable FoodProperties getFoodProperties(ItemStack stack, @Nullable LivingEntity entity) {
        var c = new CanItemContainer(stack);
        if(c.isEmpty()){return null;}
        var eatSeconds = 0f;

        for(ItemStack food: c.getItems()) {
            FoodProperties f = food.get(DataComponents.FOOD);
            if (f != null) {
                eatSeconds += f.eatSeconds();
            }
            if (food.getItem() instanceof PotionItem) {
                var p = food.getOrDefault(DataComponents.POTION_CONTENTS, PotionContents.EMPTY).potion();
                if (p.isPresent()) {
                    eatSeconds += 1.6F;
                }
            }
        }
        return new FoodProperties(0, 0, true, eatSeconds * 0.5f, Optional.empty(), List.of());
    }

    @Override
    public ItemStack finishUsingItem(ItemStack stack, Level level, LivingEntity livingEntity) {
        var c = new CanItemContainer(stack);
        for (ItemStack food: c.getItems()){
            food.finishUsingItem(level, livingEntity);
        }
        c.clearContent();
        return stack;
    }

    @Override
    public Component getName(ItemStack stack) {
        var c = new CanItemContainer(stack);
        if(stack.getOrDefault(GLYCanMod.CAN_COMPONENTS, new CanComponent(false, CanBlock.NORMAL_COLOR)).isTeto()){
            return Component.translatable("item.gly_can.can", Component.translatable("item.gly_can.can.teto"));
        }
        var l = c.getMaxItems();
        if(l.size() >= 2){
            return Component.translatable("item.gly_can.can2", l.get(0).getHoverName(), l.get(1).getHoverName());
        }else if(l.size() == 1){
            return Component.translatable("item.gly_can.can", l.getFirst().getHoverName());
        }
        return Component.translatable("item.gly_can.can", "");
    }

    @Override
    public boolean overrideOtherStackedOnMe(ItemStack stack, ItemStack other, Slot slot, ClickAction action, Player player, SlotAccess access) {
        if(action == ClickAction.PRIMARY){
            if(!other.is(ItemTags.create(ResourceLocation.parse("c:foods"))) &&
                    !other.is(ItemTags.create(ResourceLocation.parse("c:drinks"))) &&
                    !other.has(DataComponents.FOOD) &&
                    !other.has(DataComponents.POTION_CONTENTS) &&
                    !other.has(DataComponents.SUSPICIOUS_STEW_EFFECTS)){
                return false;
            }
            var c = new CanItemContainer(stack);
            var input = other.copy();
            input.setCount(1);
            if(c.addItems(input)){
                other.shrink(1);
                return true;
            }
        }else{
            var c = new CanItemContainer(stack);
            if(access.get().isEmpty() && !c.isEmpty()){
                for(int i = c.getItems().size() - 1;i >= 0;i--){
                    ItemStack stack1 = c.getItems().get(i);
                    if (!stack1.isEmpty()) {
                        access.set(c.removeItem(i, 1));
                        return true;
                    }
                }
            }
        }
        return false;
    }
}
